package connect4;
import java.util.ArrayList;
import java.util.Scanner;

public class Connect4GameHandler {
	private Connect4Model cbm ; 
	
	public Connect4GameHandler(){		
	
	}
	
	public void startGame(){
		cbm = new Connect4Model();
		cbm.initBoardData();
		printScreen();
		keyBoardListener();
	}
	
	public void printScreen(){
		System.out.println("[1, 2, 3, 4, 5, 6, 7, 8]");
		System.out.println("------------------------");
		for(ArrayList<Piece> eachRowData : cbm.getGameData()){
			System.out.print("[");
			for(Piece chess :eachRowData){
				if(chess.equals(Piece.WHITE)){
					System.out.print("W" + ", ");
				}else if(chess.equals(Piece.BLACK)){
					System.out.print("B" + ", ");
				}else{
					System.out.print("0" + ", ");
				}
			}
			System.out.println("]");
		}
		if(cbm.whoseTurn() == Piece.WHITE){
			System.out.println("------WHITE TURN-(1)----");
		}else{
			System.out.println("------BLACK TURN-(2)----");
		}
	}
	
	private void piecePalceAction(String input) {
		cbm.placeChess(input);
		printScreen();
		if(cbm.checkResult() == Result.WHITEWIN){
			System.out.println("White Win");
			System.exit(0);
		}else if(cbm.checkResult() == Result.BLACKWIN){
			System.out.println("Black Win");
			System.exit(0);
		}
		if(cbm.checkResult() == Result.DRAW){
			System.out.println("Draw Game");
			System.exit(0);
		}
	}
	
	private void keyBoardListener(){
		//scan keyboard input for 1,2,3,4,5,6,7,8 posistion
		Scanner scanner = new Scanner(System.in);
		String input = "";
		while (!input.equals("q")) {
			input = scanner.next();
			if (input.equals("1")) {
				piecePalceAction(input);
			} else if (input.equals("2")) {
				piecePalceAction(input);
			} else if (input.equals("3")) {
				piecePalceAction(input);
			} else if (input.equals("4")) {
				piecePalceAction(input);
			} else if (input.equals("5")) {
				piecePalceAction(input);
			} else if (input.equals("6")) {
				piecePalceAction(input);
			} else if (input.equals("7")) {
				piecePalceAction(input);
			} else if (input.equals("8")) {
				piecePalceAction(input);
			} else if (input.equals("r")) {
				cbm.initBoardData();
			}
		}
		scanner.close();
		
	}


}
